<?php 

 $path = drupal_get_path('module', 'department');
  drupal_add_css($path . '/css/department.css');
  drupal_add_js($path . '/js/department.js');

function department_list() {

global $language,$base_url;
if($language->language=='ta'){
$lang='/ta';
}
else{
$lang='';
}
$output .= '<div>
  <div class = "departmentfirsthalf" style = "float:left; width:50%;"><ul>';
	my_db_change(1);
	
	$result = db_query("SELECT distinct dep_id,dept_name FROM depart_mst ORDER BY dept_name ASC"); 
	$count=$result->rowcount();
    $counthalf= round($count/2)+1;
	$i=1;
	my_db_change(0);
	 
	  foreach($result as $item) {	
	  if($i == $counthalf){
		  $output .= '</ul></div><div class = "departmentsecondhalf" style = "width:50%; margin-left:50%;"><ul>';
		  $i = 1;
      }		
		 // $output .= '<div id="department_'.$item->dep_id.'" class="dpt_list_li dpt_list_li_basis"><p><a  href="'.$base_url.'/department/'.$item->dep_id.'">'.t(substr($item->dept_name,0,75)).'</a></p></div>'; 
	  $id = $item->dep_id;
  	  $li = '<li onmouseover = "javascript:focus_list(' . $id . ');" onmouseout = "javascript:blur_list(' . $id . ');" 
			   class = "dpt_list_li dpt_list_li_basis" id = "department_' . $id . '">';
	  $icon = '<div class = "li_icon li_icon_basis">&nbsp;</div>'; 
  	  $output .= $li . $icon . '<a class="normal normal_basis" href="'.$base_url.$lang.'/department/'.$id.'">'.t(substr($item->dept_name,0,75)).'</a></li>';  
		  $i += 1;
	   }

	  
	  $output .= '</ul></div>';	
	  	  

return $output;
}
function department_data_list($depid = '') {
$depid=arg(1);
global $language,$base_url;
$output = '' ;
$dept_qry = 'SELECT dept_name FROM depart_mst WHERE dep_id = '.$depid.'';
	my_db_change(1);
  	$dept_qry_result = db_query($dept_qry);
	my_db_change(0);

	foreach ($dept_qry_result as $links) {
		$output .= '<div class="titleview">' .t($links->dept_name) . '</div>';
	}
	$output .= '<div class = "department_details">';
	/*
	* Get Ministries of the department
	*/
    
	$output .= department_ministers();
	$output .= '</div><div class = "clear"></div>';
	/*
	* Get Secretaties to the Government
	*/
	$output .= secretary_government();
	$output .= '</div><div class = "clear"></div>';
	
	/*
	* Get Department Profiles
	*/
	$output .= department_profile();
	$output .= '</div><div class = "clear"></div>';
	
	/*
	* Get Organisation Details 
	*/
	$output .= organization_link();
	$output .= '</div><div class = "clear"></div>';
	
	/*
	* Get Documents Details 
	*/
	$output .= department_documents();
	$output .= '<div class = "clear"></div>';
	
	/*
	* Get Announcement Details 
	*/
	$output .= department_announcements();
	$output .= '<div class = "clear"></div>';

	/*
	* Get Details about Acts and Ordinances
	*/
	$output .= department_acts();
	$output .= '<div class = "clear"></div>';
	
	/*
	* Get Details about Rules and Regulations 
	*/
	$output .= department_rules();
	$output .= '<div class = "clear"></div>';
	
	/*
	* Get Details about Website Links
	*/
	$output .= department_website();
	$output .= '<div class = "clear"></div>';
		
return $output;
}
function department_ministers() {
global $language,$base_url,$nid,$member;
$output= '';

 $url = $base_url . '/' . drupal_get_path('module', 'department');
 $id=arg(1);
 
 	$x = 100;
	$y = 85;

 
my_db_change(1);
$newitems =array();

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'portfolio')
  ->fieldCondition('field_portfolio_department','value',$id,'=')
  ->propertyCondition('status',1)  
  ->propertyCondition('language', $language->language, '='); 
$result = $query->execute(); 

if (isset($result['node'])) {
  $news_items_nids = array_keys($result['node']); 
  
  $newitems = entity_load('node', $news_items_nids);
}
$m=0;
foreach ($newitems as $row) {
//print "<pre>"; print_r($row); print "</pre>";
    $nid = $row->nid;
	$title = $row->title;
	$ptfolio = $row->field_portfolio_portfolio['und'][0]['value'];
	$mins = $row->field_type_portfolio['und'][0]['value'];
	$member = $row->field_portfolio_member['und'][0]['value'];
	$order = $row->field_portfolio_oder['und'][0]['value'];

	$i=0;
	my_db_change(1);
		$query = db_query("SELECT DISTINCT 				 		
				a.title,c.field_mla_tele_value,e.uri,e.filename,g.field_portfolio_member_value,e.fid,f.field_mla_photo_fid,k.field_portfolio_portfolio_value from node as a 
				JOIN field_data_field_mla_tele as c ON a.nid = c.entity_id 
				JOIN field_data_field_mla_photo as f ON a.nid = f.entity_id
				JOIN file_managed as e on e.fid = f.field_mla_photo_fid			
				JOIN field_data_field_portfolio_member as g ON g.field_portfolio_member_value = cast(a.nid as text)
				JOIN field_data_field_portfolio_department as h ON h.field_portfolio_department_value = '".$id."'  
				JOIN field_data_field_portfolio_portfolio as k ON k.entity_id =h.entity_id
				AND a.status = 1 and a.language='".$language->language."'  and h.entity_id = '".$nid."' and a.nid='".$member."'  ");	
	foreach ($query as $row) {
		//	print "<pre>"; print_r($query); print "</pre>";
		$minstername = $row->title;
		$phone=$row->field_mla_tele_value ;
		$uri = $row->uri;
		$filename=$row->filename;
		$ptfolio= $row->field_portfolio_portfolio_value;
		$i++;
	}
	//$baseurl=str_replace('tnportal_demo','tncms',$base_url);
	
	$output .= '<div class ="contact_table">';
	if($m==0) {
		$output .= '<div class = "department_sub_title contact_heading">'.t('Contact details').'</div>';
	}
	$minister = '<p style = "color:#014B78"><strong>' . t($minstername) . '</strong></p>';
		$minister .= '<strong><p style = "color: #014B78;">' . t($ptfolio) . '</p></strong>';
		if( $phone != ''){
			$minister .= '<p>'.t('Phone').' : '. t($phone) . '</p>';
		}
		$output .= '<div class = "profile_row"> <div class="sub_heading"><span style = "color: #494949;">' . $mins . '</span></div> <div class = "first_col">';
		$output .= '<div class = "department_minister_image">';
					
		if(!empty($filename)){
		 $output .= '<img src="http://cms.tn.gov.in/sites/default/files/assembly/pictures/'.$filename.'" height="100" width="85" >';
		 } else {
		 $output .= '<img src="http://www.tn.gov.in/sites/all/modules/government/images/no-profile-man-medium.jpg" height="100">';		 
		 }
			$output .= '</div></div><div class = "second_col">' . t($minister) . '</div></div>';
		if( empty($minstername)){
			$output .= '<div style="padding-left:20px;">'.t('Details Not Available').'</div>';
		}
		$m++;	
}
$i=0;
if(!($newitems)) { 
		$nid = db_query("SELECT DISTINCT entity_id  from field_data_field_portfolio_department where field_portfolio_department_value = '".$id."' ORDER BY entity_id ASC ");
		foreach ($nid as $row) {
		//print "<pre>"; print_r($nid); print "</pre>";
			  $row->entity_id;	
			 $j=0;
			 if($member == '') {
				$qury = db_query("SELECT DISTINCT field_portfolio_member_value from field_data_field_portfolio_member where entity_id = '".$row->entity_id."' ORDER BY field_portfolio_member_value ASC ");
				foreach ($qury as $rows) {			
					 $rows->field_portfolio_member_value.'<br/>';
					
					$query = db_query("SELECT DISTINCT 				 		
					a.title,c.field_mla_tele_value,e.uri,e.filename,g.field_portfolio_member_value,e.fid,f.field_mla_photo_fid 
					,m.field_type_portfolio_value,k.field_portfolio_portfolio_value from node as a 
					JOIN field_data_field_mla_tele as c ON a.nid = c.entity_id 
					JOIN field_data_field_mla_photo as f ON a.nid = f.entity_id
					JOIN file_managed as e on e.fid = f.field_mla_photo_fid
					JOIN field_data_field_portfolio_member as g ON g.field_portfolio_member_value = cast(a.nid as text)
					JOIN field_data_field_portfolio_portfolio as k ON k.entity_id = '".$row->entity_id."'
					JOIN field_data_field_type_portfolio as m ON m.entity_id = k.entity_id
					JOIN field_data_field_portfolio_department as h ON h.field_portfolio_department_value = '".$id."'  
					AND a.status = 1 and a.language='en'  and h.entity_id = '".$row->entity_id."' and a.nid='".$rows->field_portfolio_member_value."'  ");
					$j++;
					foreach ($query as $links) {
						//print "<pre>"; print_r($query); print "</pre>";
						$minstername = $links->title;
						$phone=$links->field_mla_tele_value ;
						$uri = $links->uri;
						$filename=$links->filename;
						$ptfolio= $links->field_portfolio_portfolio_value;
						$mins= $links->field_type_portfolio_value;
						$i++;
					}
				}			
			}
		
			$i++;
			if($i==2 ){
				 break;
			}
		}
		$output .= '<div class ="contact_table">';
		if($m==0) {
			$output .= '<div class = "department_sub_title contact_heading">'.t('Contact details').'</div>';
		}
	
		$minister = '<p style = "color:#014B78"><strong>' . $minstername. '</strong></p>';
		$minister .= '<strong class = "strong" style = "color: #014B78;">' . $ptfolio . '</strong>';
		if( $phone != ''){
			$minister .= '<p>'.t('Phone').' : '. $phone . '</p>';
		}
		$output .= '<div class = "profile_row"> <div class="sub_heading"><span style = "color: #494949;">' . $mins . '</span></div> <div class = "first_col">';
		$output .= '<div class = "department_minister_image">';
					
		if(!empty($filename)){
		 $output .= '<img src="http://cms.tn.gov.in/sites/default/files/assembly/pictures/'.$filename.'" height="100" width="85" >';
		 } else {
		 $output .= '<img src="http://www.tn.gov.in/sites/all/modules/government/images/no-profile-man-medium.jpg" height="100">';		 
		 }
			$output .= '</div></div><div class = "second_col">' . $minister . '</div>';
		if( empty($minstername)){
			$output .= '<div style="padding-left:20px;">'.t('Details Not Available').'</div>';
		}
	
		$output .= '</div>';
}
my_db_change(0);
return $output;	
}
function secretary_government() {
global $language,$base_url;
 $id= arg(1);

$output = '<div class ="contact_table">';
$output .= '<div class="department_sub_title contact_heading" style="font-size:12px;">'.t('Secretary to Government').'</div>';
my_db_change(1);
$newitems =array();

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'contact_dir_form')
  ->fieldCondition('field_cont_dept','value',$id,'=')
  ->propertyCondition('status',1)
  //->fieldOrderBy('field_cont_salutation','value','ASC')
  ->propertyCondition('language', $language->language, '='); 
$result = $query->execute(); 

if (isset($result['node'])) {
 $news_items_nids = array_keys($result['node']); 
  
  $newitems = entity_load('node', $news_items_nids);
}
$i=0;
foreach ($newitems as $row) {
//print "<pre>"; print_r($row); print "</pre>";
	$desg = $row->field_cont_desg['und'][0]['value'];
	 $row->field_cont_salutation['und'][0]['value'];
	$landline = $row->field_cont_landline['und'][0]['value'];
	$phone = $row->field_cont_phone['und'][0]['value'];
	//$email = $row->field_cont_email['und'][0]['value'];
	$title = $row->title;
	$sal=  salutation($row->field_cont_salutation['und'][0]['value']);
	my_db_change(0);
	
		if (!empty($phone)) {
			$phone = t('Phone: ') . $phone . '';
		}
		if (!empty($row->field_cont_fax_value)) {
			$fax = ', ' . $row->field_cont_fax_value['und'][0]['value'] . ' (Fax)';    
		}
		if (!empty($row->field_cont_email)) {
			$email = t('Email: ') . $row->field_cont_email['und'][0]['value'];
		}
		if(!empty($title)){
			$minister = '<p class= "secr-det"> <strong>' . $sal . ' ' . $title . ' ( ' . $desg . ' )'.'</strong></p>';
			$minister .= '<p>'. $phone . $fax . '</p>';
			//$minister .= 'Rank:'.$links->field_cont_rank_value;
			$minister .= '<p>' . $email .'</p>';
			//$output .= '<div class = "sec_profile_row"> <div class = "sec_first_col"> <b align="center">' . $links->contact_subcat_name . '</b></div>';
			$output .= '<div class="sec_profile_row"><div class = "sec_second_col">' . $minister . '
						<p class="more_link"><strong><a href="'.$base_url.'/detail_contact/24/3 ">'.t('more...').'</a></strong></p></div></div>';
			$i++;
			if($i==2){
				break;
			}
		}else {
		   $output .= '<div style="padding-left:20px;"> No Details Available </div>';
		}
		
		 //$output .= '</div>';
}
return $output;
}
function salutation($id) {
my_db_change(1);
	$sal = db_query("select s_name from salutation_mst where s_id='".$id."'");
	foreach($sal as $links){
	  $sal= $links->s_name;
	}
	return $sal;
}
function department_profile() {
global $language,$base_url;
$id=arg(1);
my_db_change(1);
$output .= '<div class = "department_profile"><div class = "department_sub_title"> '.t('Department Profile').' </div>';
$query =db_query("select distinct d.entity_id,b.field_pro_pro_value,a.title, c.field_pro_other_value from node as a,
					field_data_field_pro_pro as b,field_data_field_pro_other as c,
					field_data_field_pro_dept as d where a.nid=b.entity_id and a.nid=c.entity_id and a.nid=d.entity_id AND d.field_pro_dept_value ='".$id."' AND a.status = 1
					AND a.language='".$language->language."'  ORDER BY a.title,d.entity_id DESC"); 

$profile_data='';
$i=0;
foreach($query as $row){
//print "<pre>"; print_r($row); print "</pre>";
	$profile_data .= '<div class="profile_content">' . $row->field_pro_pro_value . '</div>';
	$i++;
}
if (!empty($profile_data)) { $output .= $profile_data ; }
	else { $output .= '<div style="padding-left:20px;"> No Profile Details</div>'; }

my_db_change(0);
return $output;
}
function organization_link(){
global $language,$base_url;
 $id=arg(1);


	$output = '';
	$output .= '<div class = "organization_detail">';
	$output .= '<div class = "department_sub_title">'.t('Contact Particulars of Heads of Departments and Undertakings').'</div>';	
	$org .= '<div class = "org_content">';
	$org_details = '';
	$org_name= '';
	$title = '';
	$org_title='';
    $count_5 = '';
    $count = 1;
	
	
	my_db_change(1);
    $query = db_query("SELECT n.title, n.nid, b.organisation_name,d.field_orgn_depart_value ,e.field_cont_org_name_value FROM node AS n, 
						 field_data_field_orgn_type AS a, orgn_type_mst AS b ,field_data_field_orgn_depart as d,
						 field_data_field_cont_org_name as e
				   		 WHERE a.entity_id = n.nid AND CAST(d.field_orgn_depart_value AS INT) = '".$id."'
				   		 AND d.entity_id = n.nid
						 AND CAST (a.field_orgn_type_value AS INT) in (2) AND n.status = '1'
						 AND b.serial_no = CAST (a.field_orgn_type_value AS INT)AND 
						 CAST (e.field_cont_org_name_value AS INT) = n.nid	 
				   		 GROUP BY b.organisation_name, n.title, n.nid ,d.field_orgn_depart_value,e.field_cont_org_name_value 
						 ORDER BY b.organisation_name, n.title,n.nid DESC");
	my_db_change(0);
	foreach($query as $links) {
	 $count++;
		if ($org_name != $links->organisation_name) {
			$org_name = $links->organisation_name; 
			$org_details .= '<div class = "org_name">' .  $org_name . '</div>';    
		}	
		if ($title !=$links->title) {
			$title =$links->title;		   
		}	
		
		$org_details .= '<div class = "contact_particulars"><span><a href="'.$base_url.'/detail_contact/'.$links->nid.'/4/">' . $title . '</a></span></div>';	
		if($count > 3){
				break;
			} 
 	}
	
	if (!empty($org_details)) {
		$org_details .='<div  class="more_link"><strong>' . l(t('more...'), 'contact_type/4/All/4') . '</strong></div>'; 
		$org .= $org_details ;
	 }	
	$output .= $org;
	$output .= '</div>'.undertakings_link().'</div>';	
my_db_change(0);
return $output;
}
function undertakings_link() {
	global $language,$base_url;
 $id=arg(1);


	$org1 .= '<div class = "org_content">';
	$org_det = '';
	$org_nam= '';
	$tit='';
    $count = 1;
	
	
	my_db_change(1);
    $query = db_query("SELECT n.title, n.nid, b.organisation_name,d.field_orgn_depart_value ,e.field_cont_org_name_value FROM node AS n, 
						 field_data_field_orgn_type AS a, orgn_type_mst AS b ,field_data_field_orgn_depart as d,
						 field_data_field_cont_org_name as e
				   		 WHERE a.entity_id = n.nid AND CAST(d.field_orgn_depart_value AS INT) = '".$id."'
				   		 AND d.entity_id = n.nid
						 AND CAST (a.field_orgn_type_value AS INT) in (3) AND n.status = '1'
						 AND b.serial_no = CAST (a.field_orgn_type_value AS INT)AND 
						 CAST (e.field_cont_org_name_value AS INT) = n.nid
						  AND a.revision_id = n.vid AND d.revision_id = n.vid				   		 
				   		 GROUP BY b.organisation_name, n.title, n.nid ,d.field_orgn_depart_value,e.field_cont_org_name_value 
						 ORDER BY b.organisation_name, n.title,n.nid DESC");
	my_db_change(0);
	foreach($query as $links) {
	$count++;
		if ($org_nam != $links->organisation_name) {
			$org_nam = $links->organisation_name; 
			$org_det .= '<div class = "org_name">' .  $org_nam . '</div>';    
		}	
		if ($tit !=$links->title) {
			$tit =$links->title;		   
		}	
		
		$org_det .= '<div class = "contact_particulars"><span><a href="'.$base_url.'/detail_contact/'.$links->nid.'/5/">' . $tit . '</a></span></div>';	
		if($count > 3){
				break;
			} 
		
 	}
	
	if (!empty($org_det)) {
		$org_det .='<div  class="more_link"><strong>' . l(t('more...'), 'contact_type/5/All/5') . '</strong></div>'; 
		$org1 .= $org_det ;
	 }
	
	$output .= $org1;
	my_db_change(0);
return $output;
}
function department_documents() {
global $language,$base_url;
$id=arg(1);
//$doc_types = document_types();
$doc_details = array();
$doc_result = '';

$count = 0;
my_db_change(1);

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')
	  ->fieldCondition('field_department','value',$id,'=')
	  ->fieldOrderBy('field_doc_year','value','DESC')	  
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', $language->language, '=')
	  ->range(0,3); 
 


$result = $query->execute(); 

if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 
	  
	  $items = entity_load('node', $news_items_nids);
}
//print "<pre>"; print_r($items); print "</pre>";
	foreach($items as $links) {
			$count += 1;
			if ($links->field_doctype['und'][0]['value'] == 'online') {
				$link =  $links->field_docurl['und'][0]['value'];
			}
			else if($links->field_doctype['und'][0]['value'] == 'download') {
				$wrapper = file_stream_wrapper_get_instance_by_uri($links->field_docupload['und'][0]['uri']);
				if($wrapper == true){
					 $path = $wrapper->getDirectoryPath() . "/" . file_uri_target($links->field_docupload['und'][0]['uri']);
				}
			 $link="'http://cms.tn.gov.in/$path'";
			}
			$doc_result .= '<p class = "dept_list"><a href='. $link.' target="_blank">' .$links->title.'</a></p>';	
	}
	
	if ($count > 4) {
		break;
	}

	my_db_change(0);
	if (!empty($doc_result)) {
		$doc = '<div class = "department_document">';
		$doc .= '<div class = "department_sub_title">'.t('Documents:').'</div>';
		$doc .= '<div class = "doc_links">';
		$doc .= $doc_result;
		$doc .='<div class = "more_link">' . l(t('more...'), 'documents/dept/'.arg(1)) . '</div></div></div>';
	}
return $doc;
}
function department_announcements() {
global $language,$base_url;
	$id=arg(1);
	$doc = '';
	$doc_result = '';
	
	my_db_change(1);	
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
		  ->entityCondition('bundle', 'announcements')
		  ->fieldCondition('field_anndept','value',$id,'=')
		  ->propertyCondition('status',1)
		  ->propertyCondition('language', $language->language, '=')
		  ->range(0,3); 	
 
	$result = $query->execute(); 

	if (isset($result['node'])) {
		  $news_items_nids = array_keys($result['node']); 
		  
		  $items = entity_load('node', $news_items_nids);
	}
	my_db_change(0);
	foreach($items as $links) {
		if ($links->field_annavail['und'][0]['value'] == 'OnlineAvailable') {
			$link =  $links->field_annurl['und'][0]['value'];
		}
		else if($links->field_annavail['und'][0]['value'] == 'Downloadable') {
			$wrapper = file_stream_wrapper_get_instance_by_uri($links->field_anndown['und'][0]['uri']);
			if($wrapper == true){
				 $path = $wrapper->getDirectoryPath() . "/" . file_uri_target($links->field_anndown['und'][0]['uri']);
			}
			 $link="'http://cms.tn.gov.in/$path'";
		}
		else {
			$link = '';
		}
		$doc_result .= '<p class = "dept_list"><a href='. $link.' target="_blank">' .$links->title.'</a></p>';
	}
	if (!empty($doc_result)) {
		$doc = '<div class = "department_announcements">';
		$doc .= '<div class = "department_sub_title">'.t('Announcements:').'</div>';
		$doc .= '<div class = "ann_links">';  
		$doc .= $doc_result;
		$doc .='<div class = "more_link">' . l(t('more...'), 'announcements/'.arg(1)) . '</div></div></div>';	
	}
	return $doc;
}
function department_acts() {
global $language,$base_url;
	$id=arg(1);
	$doc = '';
	$doc_result = '';
	
	my_db_change(1);
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')     
		  ->entityCondition('bundle', 'actform')
		  ->fieldCondition('field_act_dpt','value',$id,'=')
		  ->propertyCondition('status',1)
		 // ->propertyCondition('language', $language->language, '=')
		  ->range(0,3); 	
 
	$result = $query->execute(); 

	if (isset($result['node'])) {
		  $news_items_nids = array_keys($result['node']); 
		  
		  $items = entity_load('node', $news_items_nids);
	}
	//print "<pre>"; print_r($items); print "</pre>";
	my_db_change(0);
	foreach($items as $links) {
		if ($links->field_act_details['und'][0]['value'] == 'online') {
			$link =  $links->field_act_url['und'][0]['value'];
		}
		else if($links->field_act_details['und'][0]['value'] == 'download') {
			$wrapper = file_stream_wrapper_get_instance_by_uri($links->field_act_file_upload['und'][0]['uri']);
			if($wrapper == true){
				 $path = $wrapper->getDirectoryPath() . "/" . file_uri_target($links->field_act_file_upload['und'][0]['uri']);
			}
		 $link="'http://cms.tn.gov.in/$path'";
		}
		$doc_result .= '<p class = "dept_list"><a href='. $link.' target="_blank">' .$links->title.'</a></p>';	
	}
	if (!empty($doc_result)) {
		$doc = '<div class = "department_document">';
		$doc .= '<div class = "department_sub_title">'.t('Acts and Ordinances:').'</div>';
		$doc .= '<div class = "doc_links">';  
		$doc .= $doc_result;
		$doc .='<div class = "more_link">' . l(t('more...'), 'acts/dept/') . '</div></div></div>';	
	}
	return $doc;
}
function department_rules() {
global $language,$base_url;
	$id=arg(1);
	$doc = '';
	$doc_result = '';
	
	my_db_change(1);
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
		  ->entityCondition('bundle', 'rulesandregulation')
		  ->fieldCondition('field_rules_dep','value',$id,'=')
		  ->propertyCondition('status',1)
		 // ->propertyCondition('language', $language->language, '=')
		  ->range(0,3); 	
 
	$result = $query->execute(); 

	if (isset($result['node'])) {
		  $news_items_nids = array_keys($result['node']); 
		  
		  $items = entity_load('node', $news_items_nids);
	}
	//sprint "<pre>"; print_r($items); print "</pre>";
	my_db_change(0);
	foreach($items as $links) {
		if ($links->field_rules_avail['und'][0]['value'] == 'OnlineAvailable') {
			$link =  $links->field_rules_url['und'][0]['value'];
		}
		else if($links->field_rules_avail['und'][0]['value'] == 'Downloadable') {
		 $links->field_rules_fileup['und'][0]['uri'];		
		$wrapper = file_stream_wrapper_get_instance_by_uri($links->field_rules_fileup['und'][0]['uri']);
			if($wrapper == true){
				 $path = $wrapper->getDirectoryPath() . "/" . file_uri_target($links->field_rules_fileup['und'][0]['uri']);
			}
			 $link="'http://cms.tn.gov.in/$path'";
		}
		else {
			$link = '';
		}
		$doc_result .= '<p class = "dept_list"><a target="_blank" href='. $link.' >' .$links->title.'</a></p>';
	}
	if (!empty($doc_result)) {
		$doc = '<div class = "department_document">';
		$doc .= '<div class = "department_sub_title">'.t('Rules and Regulations:').'</div>';
		$doc .= '<div class = "doc_links">';  
		$doc .= $doc_result;
		$doc .='<div class = "more_link">' . l(t('more...'), 'rules/dept/'.arg(1)) . '</div></div></div>';	
	}
	return $doc;
}
function department_website() {
global $language,$base_url;
	$id=arg(1);
	$doc = '';
	$doc_result = '';
	
	my_db_change(1);
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
		  ->entityCondition('bundle', 'website_entry')
		  ->fieldCondition('field_web_dept','value',$id,'=')
		  ->fieldOrderBy('field_web_keyword','value','ASC')
		  ->propertyCondition('status',1)
		  //->propertyCondition('language', $language->language, '=')
		  ->range(0,5); 	
 
	$result = $query->execute(); 

	if (isset($result['node'])) {
		  $news_items_nids = array_keys($result['node']); 
		  
		  $items = entity_load('node', $news_items_nids);
	}
	//print "<pre>"; print_r($items); print "</pre>";
	my_db_change(0);
	foreach($items as $links) {
		 $link = $links->field_web_url['und'][0]['value'];
		$doc_result .= '<p class = "dept_list"><a target="_blank" href='.$link .' >' .$links->title.'</a></p>';
	}
	if (!empty($doc_result)) {
		$doc = '<div class = "department_related">';
		$doc .= '<div class = "department_sub_title">'.t('Websites / Related links:').'</div>';
		$doc .= '<div class = "doc_links">';  
		$doc .= $doc_result;
		$doc .='<div class = "more_link">' . l(t('more...'), 'website_directory/'.arg(1)) . '</div></div></div>';	
	}
	return $doc;
}
function document_types() {	

	$doc_types = array ('Information Guide on Welfare Schemes', 'Citizen Character', 'RTI:Proactive Disclosure Document', 'Annual Report',
						'Policy Document', 'Performance Budget', 'Annual Policy Note', 'State Budget', 'Budget Related(White Paper/Accounts Statement)',
						'Plan Document',  'Guidelines', 'Announcements', 'Reports/Surveys', 'Statistical Report', 'Magazine/Bulletins',
						'Gazette Publication', 'Others');
	return $doc_types;
}





?>